package com.yxj.lib_exo;

import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.ui.StyledPlayerView;

/**
 * 播放器
 */
public class ExoPlayerView extends FrameLayout {

    //视频播放器
    public StyledPlayerView styledPlayerView;
    //创建播放器实例
    private ExoPlayer player;

    public ExoPlayerView(@NonNull Context context) {
        this(context, null);
    }

    public ExoPlayerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ExoPlayerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addVideoView();
    }


    private void addVideoView() {
        styledPlayerView = new StyledPlayerView(getContext());
        styledPlayerView.setUseController(false);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER);
        addView(styledPlayerView, params);
        bindPlay();
    }


    /**
     * 绑定控制器
     */
    public ExoPlayerControl control;
    public void bindControl(ExoPlayerControl control) {
        this.control = control;
        if (control==null){
            control = new ExoPlayerControl(getContext());
        }
        control.setPlayer(player);
        addView(control, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }


    /**
     * 创建播放器实例
     */
    private void bindPlay() {
        player = new ExoPlayer.Builder(getContext()).build();
        // 将播放器与UI组件绑定
        styledPlayerView.setPlayer(player);
    }


    public static void showLog(String s) {
        Log.v("[showLog]", "-> s = " + s);
    }


    /**
     * 是否准备完成之后自动播放
     *
     * @param b
     */
    public void setAutoPlay(boolean b) {
        if (player == null) return;
        // 开始播放（默认自动播放，可通过playWhenReady控制）
        player.setPlayWhenReady(b);
    }


    /**
     * 设置视频资源
     * 缓冲准备
     *
     * @param dataSource
     */
    private Uri dataSource;

    public void setDataSource(Uri dataSource) {
        if (dataSource != null) {
            this.dataSource = dataSource;
            player.setMediaItem(MediaItem.fromUri(dataSource));
            // 准备播放
            player.prepare();
            //是否准备完成之后自动播放
            setAutoPlay(false);
        }
    }


    /**
     * 如果出现错误
     * <p>
     * 再次加载资源
     */
    public void repeatDataSourceAgain() {
        setDataSource(dataSource);
    }


    /**
     * 播放器构造器
     */
    public ExoPlayer getPlayer() {
        return player;
    }


    public void play() {
        if (player == null) return;
        player.play();
        ExoPlayerManager.newInstance().play(this);
    }


    /**
     * 暂停
     */
    public void pause() {
        if (player == null) return;
        player.pause();
        if (control != null) {
            control.removeCallbacks();
        }
    }

    /**
     * 释放重复播放
     *
     * @param r
     */
    public void setRepeatMode(int r) {
        if (player == null) return;
        player.setRepeatMode(r);
    }


    public void seekTo(long positionMs) {
        if (player == null) return;
        player.seekTo(positionMs);
    }


    /**
     * 释放资源
     */
    public void release() {
        if (player == null) return;
        player.release();
    }


    /**
     * 添加回调
     *
     * @param listener
     */
    public void setListener(Player.Listener listener) {
        if (player == null) return;
        player.addListener(listener);
    }
}
